﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
namespace DataSyn.Forms
{
    public partial class FormConnectionSetting : DataSyn.Forms.FormBase
    {
        public FormConnectionSetting()
        {
            InitializeComponent();
        }

        private void barButtonItem_New_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (BaseLibrary.UControls.DBSetting.Form_Setting form = new BaseLibrary.UControls.DBSetting.Form_Setting())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    var conn = new BaseLibrary.CDataContext.ConnectionSetting.Conn() { Provider = form.Provider, Connection = form.Connection };
                    conn.DataName = "master";
                    var tem = new DAL.DB.Tables.TConnection() { Provider = conn.Provider, ConnectionString = conn.Connection };
                    connectionSettingBindingSource.Add(tem);
                    MDL.CPublic.dc.Save(tem);
                }
            }
        }
        private void FormConnectionSetting_Load(object sender, EventArgs e)
        {
            connectionSettingBindingSource.DataSource = MDL.CPublic.dc.TConnection.ToList();
        }

        private void FormConnectionSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            gridView1.CloseEditor();
            connectionSettingBindingSource.EndEdit();
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            MessageBox.Show(e.RowHandle.ToString());
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var tem = connectionSettingBindingSource.Current as DAL.DB.Tables.TConnection;
            if (tem == null)
                return;
            switch (e.Button.Caption)
            {
                case "修改":
                    using (BaseLibrary.UControls.DBSetting.Form_Setting form = new BaseLibrary.UControls.DBSetting.Form_Setting() { Provider = tem.Provider, Connection=tem.ConnectionString })
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            var conn = new BaseLibrary.CDataContext.ConnectionSetting.Conn() { Provider = form.Provider, Connection = form.Connection };
                            conn.DataName = "master";
                            tem.Provider = conn.Provider;
                            tem.ConnectionString = conn.Connection;
                            MDL.CPublic.dc.Save(tem);
                        }
                    }
                    break;
                case "删除":
                    tem.Delete();
                    MDL.CPublic.dc.Save(tem);
                    connectionSettingBindingSource.Remove(tem);
                    break;
            }
        }
        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            var tem = connectionSettingBindingSource.Current as DAL.DB.Tables.TConnection;
            if (tem == null) return;
            MDL.CPublic.dc.Save(tem);
        }
    }
}
